using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EnvDTE;

namespace SPVisualDev
{
    public partial class DialogArtifactConfig : Form
    {
        private Project _project;

        public DialogArtifactConfig(Project project)
        {
            InitializeComponent();

            _project = project;
            this.chkLCEnablePageDebuging.CheckedChanged += new EventHandler(chkLCEnablePageDebuging_CheckedChanged);
        }

        private void chkLCEnablePageDebuging_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.Created)
                {
                    if (chkLCEnablePageDebuging.Checked)
                    {
                        if (PropGCAssemblyBuildType == EnumBuildType.ProjectDefualt)
                        {
                            MessageBox.Show("Project build output must be changed to GAC or App bin in the global config tab.", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        else
                        {
                            MessageBox.Show("Important! The IIS application pool account which runs the selected web application must have full read access to the project path. Please change if not already set.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }


        public string PropProjectDefaultNamespace;

        public string PropGCWebPartsNSsuffix
        {
            get { return txtGCWebPartsNSsuffix.Text; }
            set { txtGCWebPartsNSsuffix.Text = value; }
        }


        public string PropGCWebCustomConstrolNSsuffix
        {
            get { return txtGCWebCustomControlsNSsuffix.Text; }
            set { txtGCWebCustomControlsNSsuffix.Text = value; }
        }
        public bool PropGCWPInFeatureFolder
        {
            get { return chkGCWPInFeatureFolder.Checked; }
            set { chkGCWPInFeatureFolder.Checked = value; }
        }

        public string PropGCMasterPagesNSsuffix
        {
            get { return txtGCMasterPagesNSsuffix.Text; }
            set { txtGCMasterPagesNSsuffix.Text = value; }
        }

        public string PropGCASPXPagesNSsuffix
        {
            get { return txtGCASPXPagesNSsuffix.Text; }
            set { txtGCASPXPagesNSsuffix.Text = value; }
        }

        public string PropGCEntitiesNSSuffix
        {
            get { return txtGCEntitiesNSSuffix.Text; }
            set { txtGCEntitiesNSSuffix.Text = value; }
        }
        public bool PropGCEntitiesInFeature
        {
            get { return chkGCEntitiesInFeature.Checked; }
            set { chkGCEntitiesInFeature.Checked = value; }
        }


        public string PropGCEventNSSuffix
        {
            get { return txtGCEventRecNSSuffix.Text; }
            set { txtGCEventRecNSSuffix.Text = value; }
        }
        public bool PropGCEventRecInFeature
        {
            get { return chkGCEventRecInFeature.Checked; }
            set { chkGCEventRecInFeature.Checked = value; }
        }

        public string PropWSPSolutionId
        {
            get { return txtWSPSolutionId.Text; }
            set { txtWSPSolutionId.Text = value; }
        }

        public string PropWSPAdditionalSwtches
        {
            get { return txtWSPAdditionalSwitches.Text; }
            set { txtWSPAdditionalSwitches.Text = value; }
        }

        public string PropLCTargetURL
        {
            get { return txtLCTargetURL.Text; }
            set { txtLCTargetURL.Text = value; }
        }

        public EnumBuildType PropGCAssemblyBuildType
        {
            get
            {
                if (rdoGCBuildAppBin.Checked)
                    return EnumBuildType.AppBin;
                else if (rdoGCBuildGAC.Checked)
                    return EnumBuildType.GAC;
                else
                    return EnumBuildType.ProjectDefualt;
            }
            set
            {
                if (value == EnumBuildType.AppBin)
                    rdoGCBuildAppBin.Checked = true;
                else if (value == EnumBuildType.GAC)
                    rdoGCBuildGAC.Checked = true;
                else
                    rdoGCBuildProjectDefault.Checked = true;
            }
        }

        public string PropGCProductVersion
        {
            get { return lblGCProductVersion.Text; }
            set { lblGCProductVersion.Text = value; }
        }

        public bool PropLCPageDebugingEnabled
        {
            get { return chkLCEnablePageDebuging.Checked; }
            set { chkLCEnablePageDebuging.Checked = value; }
        }

        public string AssemblyFullName
        {
            get { return txtAssemblyFullName.Text; }
            set { txtAssemblyFullName.Text = value; }
        }

        private bool m_HasAssemblyOriginatorKeyFile;
        public bool HasAssemblyOriginatorKeyFile
        {
            get { return m_HasAssemblyOriginatorKeyFile; }
            set { m_HasAssemblyOriginatorKeyFile = value; }
        }

        private List<DialogIncludedAssemblies.IncludeAssemblyStruct> m_IncludedAssemblies;
        public List<DialogIncludedAssemblies.IncludeAssemblyStruct> PropWSPIncludedAssemblies
        {
            get { return m_IncludedAssemblies; }
            set { m_IncludedAssemblies = value; }
        }

        public enum EnumBuildType
        {
            ProjectDefualt,
            GAC,
            AppBin
        }

        public string PropWSPBuilderPath
        {
            get { return txtWSPBuilderPath.Text; }
            set { txtWSPBuilderPath.Text = value; }
        }

        public string PropLCSkipFilesPushDown
        {
            get { return txtLCSkipFilesPushDown.Text; }
            set { txtLCSkipFilesPushDown.Text = value; }
        }

        private void btnLCPickTarget_Click(object sender, EventArgs e)
        {
            try
            {
                using (DialogSharePointExplorer explorer = new DialogSharePointExplorer())
                {
                    explorer.RootUrl = "http://localhost";
                    explorer.Message = "Choose a site to use as target for viewing, debugging and activating features.";
                    explorer.ShowLists = false;
                    explorer.UseWOW64BitCompatibleMode = Common.CheckIf32bitOn64();

                    DialogResult res = explorer.ShowDialog();
                    if (res != DialogResult.OK)
                        return;

                    if (!string.IsNullOrEmpty(explorer.SelectedUrl))
                        if (explorer.SelectedUrl.ToLower().StartsWith("http://"))
                            PropLCTargetURL = explorer.SelectedUrl;
                        else
                            MessageBox.Show("No valid location selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }

        private void DialogArtifactConfig_Load(object sender, EventArgs e)
        {
            //Disable options that is not supported on 64 bit sharepoint inst.
            //if (Common.CheckIf32bitOn64())
            //{
                this.txtLCTargetURL.Enabled = true;
                this.chkLCEnablePageDebuging.Enabled = true;
                this.btnLCPickTarget.Enabled = true;
            //}
        }


        private void rdoGCBuildGAC_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rdoGCBuildGAC.Checked && this.Modal && !HasAssemblyOriginatorKeyFile)
                    MessageBox.Show("This option requires an public key token to be assigned to this assembly build before it can be registered correctly in the GAC. Please assign a key directly after finishing the artifact configuration.", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }

        private void btnFolderInclExcl_Click(object sender, EventArgs e)
        {
            try
            {
                using (DialogTreeSelector tree = new DialogTreeSelector())
                {
                    ProjectItem twelve_hive = null;
                    try
                    {
                        twelve_hive = _project.ProjectItems.Item("12");
                    }
                    catch { }

                    if (twelve_hive != null)
                    {

                        TreeNode node = new TreeNode("12");
                        node.Name = "12";
                        node.Expand();

                        ProcessUIHierarchy(node, twelve_hive.ProjectItems);

                        tree.TreeView.BeginUpdate();
                        tree.TreeView.Nodes.Add(node);
                        tree.TreeView.TopNode = node;
                        tree.TreeView.EndUpdate();

                        tree.CheckedNodes = Common.GetUserSettingsKey(_project, "ItemExclIncl");

                        tree.StartPosition = FormStartPosition.CenterScreen;
                        tree.ShowDialog();

                        Common.SetUserSettingsKey(_project, "ItemExclIncl", tree.CheckedNodes);
                        //MessageBox.Show(tree.CheckedNodes);

                    }


                }

            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }

        private void ProcessUIHierarchy(TreeNode node, ProjectItems items)
        {
            foreach (ProjectItem item in items)
            {
                if (item.ProjectItems.Count > 0)
                {
                    TreeNode new_node = new TreeNode(item.Name);
                    new_node.Name = item.Name;

                    ProcessUIHierarchy(new_node, item.ProjectItems);

                    node.Nodes.Add(new_node);
                }
                else
                {
                    node.Nodes.Add(item.Name, item.Name);
                }
            }
        }

        private void btnWSPNewSolutionId_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Do you want to generate a new WSP solution id?", "Prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                    return;

                PropWSPSolutionId = Guid.NewGuid().ToString("D").ToLower();
            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }

        private void btnWSPEditIncludedAssemblies_Click(object sender, EventArgs e)
        {
            try
            {
                using (DialogIncludedAssemblies dlg = new DialogIncludedAssemblies())
                {
                    dlg.Assemblies = m_IncludedAssemblies;
                    DialogResult result = dlg.ShowDialog(this);

                    if (result == DialogResult.OK)
                    {
                        m_IncludedAssemblies = dlg.Assemblies;
                    }
                }
            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }

        }

        private void btnWSPBuilderPath_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult res = openFileDialog.ShowDialog(this);

                if (res == DialogResult.OK)
                {
                    PropWSPBuilderPath = openFileDialog.FileName;
                }
            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }

        }

    }
}